package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo9 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()){
            //链接创建时是默认自动提交的，意味着每当执行一条DML语句就会COMMIT一次
            connection.setAutoCommit(false);//如果需要自己控制事务，需要关闭自动提交

            Statement statement = connection.createStatement();
            String sql = "INSERT INTO userinfo(username,password,nickname,age) " +
                    "VALUES('李四','123456','老四',33)";
            statement.executeUpdate(sql);

            System.out.println("插入完毕");

            ResultSet rs = statement.executeQuery("SELECT username FROM userinfo");
            while (rs.next()){
                System.out.println(rs.getString("username"));
            }


            connection.commit();//之前的操作全部算数了
//            connection.rollback();
            //之后会开启新的事务
            //开始新的增删改操作
            //要么Commit:将上次commit之后的操作全部算数
            //要么ROLLBACK:将上次commit之后的操作全部撤销


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
